package com.sky.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class SmsServiceImpl {

    @Autowired
    private RestTemplate restTemplate;

    private static final String ACCESS_KEY_ID = "LTAI5tLhAvpo4eJDkCScX9js";
    private static final String ACCESS_KEY_SECRET = "UQR59ws3dgxnZf0MRbdmszyw3goapk";
    private static final String ENDPOINT = "https://dysmsapi.aliyuncs.com/";

    public void sendSms(String phoneNumber, String signName, String templateCode, String templateParam) {
        String requestUrl = UriComponentsBuilder.fromHttpUrl(ENDPOINT)
                .queryParam("AccessKeyId", ACCESS_KEY_ID)
                .queryParam("AccessKeySecret", ACCESS_KEY_SECRET)
                .queryParam("Action", "SendSms")
                .queryParam("PhoneNumbers", phoneNumber)
                .queryParam("SignName", signName)
                .queryParam("TemplateCode", templateCode)
                .queryParam("TemplateParam", templateParam)
                .toUriString();

        String response = restTemplate.postForObject(requestUrl, null, String.class);
        System.out.println("Response: " + response);
    }
}

